//
//  AddMainPhoneViewController.m
//  LFCWarmSystem
//
//  Created by 李福才 on 16/4/24.
//  Copyright © 2016年 李福才. All rights reserved.
//

#import "AddMainPhoneViewController.h"
#import "UIViewController+extension.h"
#import "DBOperation.h"

@interface AddMainPhoneViewController (){
    UIScrollView *mainView;
    UITextField *textFieldFrist;
    UITextField *textFieldSecond;
    BOOL isEdit;
}

@end

@implementation AddMainPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:nil leftBarItemsTitle:@[NSLocalizedString(@"Cancel_Sample", @"取消")] rightBarItemsTitle:@[NSLocalizedString(@"Complete_Sample", @"完成")]];
    [self uiInit];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)uiInit{
    self.automaticallyAdjustsScrollViewInsets = NO;
    mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(10*WIDTHSCALE, 64+20*WIDTHSCALE, self.view.frame.size.width-2*10*WIDTHSCALE, self.view.frame.size.height-64-(64+20)*WIDTHSCALE)];
    mainView.layer.cornerRadius = 5.0;
    mainView.backgroundColor = TABLEVIEWCOLOR;
    [self.view addSubview:mainView];
        
    UILabel *lblMain = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, mainView.frame.size.width, mainView.frame.size.height/8.0)];
    lblMain.textAlignment = NSTextAlignmentCenter;
    lblMain.textColor = COLOR_MAINHEAD;
    lblMain.font = TABLEHEADFONT;
    
    UILabel *lblSectionHeadFrist = [[UILabel alloc] initWithFrame:CGRectMake(10*WIDTHSCALE, lblMain.frame.origin.y+lblMain.frame.size.height, mainView.frame.size.width-2*10*WIDTHSCALE, mainView.frame.size.height/12.0)];
    lblSectionHeadFrist.text = NSLocalizedString(@"MainAccountName_Sample",@"  主机名");
    lblSectionHeadFrist.textColor = TEXTCOLOR;
    lblSectionHeadFrist.font = TABLESECTIONFONT;
    
    
    
    textFieldFrist = [[UITextField alloc] initWithFrame:CGRectMake(10*WIDTHSCALE, lblSectionHeadFrist.frame.origin.y+lblSectionHeadFrist.frame.size.height, mainView.frame.size.width-2*10*WIDTHSCALE, TEXTHEIGHT)];
    [textFieldFrist setBackgroundColor:TEXTFIELDCOLOR];
    textFieldFrist.layer.cornerRadius = 5.0;
    textFieldFrist.delegate = self;
    textFieldFrist.textColor = [UIColor whiteColor];
    textFieldFrist.placeholder = NSLocalizedString(@"PleaseEnterPhoneName_Sample",@"  请输入主机名");
    [textFieldFrist setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [textFieldFrist setValue:TEXTFONT forKeyPath:@"_placeholderLabel.font"];
    
    
    UILabel *lblSectionHeadSecond = [[UILabel alloc] initWithFrame:CGRectMake(10*WIDTHSCALE, textFieldFrist.frame.origin.y+textFieldFrist.frame.size.height, mainView.frame.size.width-2*10*WIDTHSCALE, mainView.frame.size.height/12.0)];
    lblSectionHeadSecond.text = NSLocalizedString(@"MainAccountPhoneNum_Sample",@"  主机号码");
    lblSectionHeadSecond.textColor = TEXTCOLOR;
    lblSectionHeadSecond.font = TABLESECTIONFONT;
    
    
    textFieldSecond = [[UITextField alloc] initWithFrame:CGRectMake(10*WIDTHSCALE, lblSectionHeadSecond.frame.origin.y+lblSectionHeadSecond.frame.size.height, mainView.frame.size.width-2*10*WIDTHSCALE, TEXTHEIGHT)];
    [textFieldSecond setBackgroundColor:TEXTFIELDCOLOR];
    textFieldSecond.layer.cornerRadius =5.0;
    textFieldSecond.delegate = self;
    textFieldSecond.textColor = [UIColor whiteColor];
    textFieldSecond.placeholder = NSLocalizedString(@"PleaseEnterMainPhoneNum_Sample",@"  请输入主机号码");
    [textFieldSecond setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [textFieldSecond setValue:TEXTFONT forKeyPath:@"_placeholderLabel.font"];

    
    [mainView addSubview:lblMain];
    [mainView addSubview:lblSectionHeadFrist];
    [mainView addSubview:textFieldFrist];
    [mainView addSubview:lblSectionHeadSecond];
    [mainView addSubview:textFieldSecond];
    if (!_model) {
        lblMain.text = NSLocalizedString(@"AddMainAccount_sample",@"添加主机账户");
        _model = [[ModelMainPhone alloc] init];
    }else{
        isEdit = TRUE;
        textFieldFrist.text = _model.phoneName;
        textFieldSecond.text = _model.phoneNumber;
    }
    mainView.contentSize = CGSizeMake(self.view.frame.size.width-2*10*WIDTHSCALE, textFieldSecond.frame.origin.y+textFieldSecond.frame.size.height+10);
}

//按钮点击事件
-(void)rightAction:(id)sender{
    if ([textFieldFrist.text isEqualToString:@""]&&[textFieldSecond.text isEqualToString:@""]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"WrongAddAccount",@"添加账号出错") message:NSLocalizedString(@"Phone Name Or Number Can Not Be Empty",@"主机名或主机号码不能为空!") preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Sure_Sample",  @"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction *alertAction){
            NSLog(@"点击确认");
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel_Sample", @"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction *alertAction){
            NSLog(@"点击取消");
        }]];
        [self presentViewController:alert animated:YES completion:^{
           
        }];
        return ;
    }
    _model.phoneName = textFieldFrist.text;
    _model.phoneNumber = textFieldSecond.text;
    if (isEdit) {
        [self updateDB];
    }else{
        [self insertDB];
    }
    [self dismissViewControllerAnimated:YES completion:^{
        if ([_delegate respondsToSelector:@selector(addMainPhoneSuccssed)]) {
             [_delegate addMainPhoneSuccssed];
        }
    }];
    
}
-(void)backAction:(id)sender{
    [self dismissViewControllerAnimated:YES completion:^{}];
}
#pragma mark-UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([textField isEqual:textFieldSecond]) {
        textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    NSLog(@"TextField Begin");
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"TextField end Endit");
    if ([textField isEqual:textFieldFrist]) {
        _model.phoneName = textField.text;
        return;
    }
    if ([textField isEqual:textFieldSecond]) {
        _model.phoneNumber = textField.text;
        return;
    }
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return  YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return YES;
}


-(void)dismissKeyboard{
    [self  resignFirstResponder];
}



-(void)updateDB{
    DBOperation *dbo = [DBOperation getDBOperateInstance];
    [dbo updateMainAccountWithModel:_model];
}
-(void)insertDB{
    DBOperation *dbo = [DBOperation getDBOperateInstance];
    [dbo insertMainAccountWithModel:_model];
}


//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
- (void)keyboardWasShown:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    //kbSize即為鍵盤尺寸 (有width, height)
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
    NSLog(@"keyboard Height:%f",kbSize.height);
    //输入框位置动画加载
    [self begainMoveUpAnimation:kbSize.height];
}
//当键盘隐藏的时候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
    //do something
    [self begainMoveUpAnimation:0];
}
- (void)begainMoveUpAnimation:(CGFloat)keyBoardHeight {
    //开始动画
    [UIView beginAnimations:nil context:nil];
    //设定动画持续时间
    [UIView setAnimationDuration:0.5];
    //动画的内容
    NSLog(@"heheheheheh%f,%f",self.view.frame.size.width,self.view.frame.size.height);
    CGRect frame = CGRectMake(10*WIDTHSCALE, 64+20*WIDTHSCALE,  self.view.frame.size.width-2*10*WIDTHSCALE, self.view.frame.size.height-64-20*WIDTHSCALE-10-(keyBoardHeight>1?keyBoardHeight:64*WIDTHSCALE));
    [mainView setFrame:frame];
    
    //动画结束
    [UIView commitAnimations];
}

@end
